﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.IO;
using System.Windows.Forms;

namespace JYStockAnalyser.DataGeter
{
    public partial class frmGet11001 : Form
    {
        //WebBrowser webb1;
        //HtmlDocument htmldoc1;
        System.Threading.Thread th;
        string beginStockNO;
        string endStockNO;

        public frmGet11001()
        {
            InitializeComponent();
            //关闭线程检查
            Control.CheckForIllegalCrossThreadCalls = false;
            //webb1 = new WebBrowser();
            //webb1.Navigate("about:blank");
            //htmldoc1 = webb1.Document.OpenNew(true);
            m_enterReadyState();
        }

        private void Log(string message)
        {
            StreamWriter sw = new StreamWriter("log.txt", true);
            sw.WriteLine(message);
            sw.Close();
        }       

        private void btnStart_Click(object sender, EventArgs e)
        {
            m_enterGettingState();

            beginStockNO = this.txtBeginStock.Text.Trim();
            endStockNO = this.txtEndStock.Text.Trim();
            th = new System.Threading.Thread(new System.Threading.ThreadStart(getData11001));
            th.Start();            
        }

        private void m_enterGettingState()
        {
            this.txtBeginStock.Enabled = false;
            this.txtEndStock.Enabled = false;
            this.dateTimePicker1.Enabled = false;
            this.btnStart.Enabled = false;
            this.btnStop.Enabled = true;
        }

        void getData11001()
        {
            List<string> stockNOs = new List<string>();
            string[] stocks1 = DataTool.GetAllStocks(beginStockNO, endStockNO, 1);
            string[] stocks2 = DataTool.GetAllStocks(beginStockNO, endStockNO, 2);

            stockNOs.AddRange(stocks1);
            stockNOs.AddRange(stocks2);

            foreach (string stock in stockNOs)
            {
                bool succsess = false;

                string strWebContent = string.Empty;

                //while (!succsess)
                //{
                //    try
                //    {
                        strWebContent = Tool.GetContentFromUrll(String.Format("http://stockdata.stock.hexun.com/{0}.shtml", stock));
                        succsess = true;
                    //}
                    //catch (Exception ex)
                    //{
                    //    Log(ex.ToString());
                    //    lbStatus.Items.Add(String.Format("连接超时,5秒后重新连接......", stock));                        
                    //    lbStatus.SelectedIndex = lbStatus.Items.Count - 1;
                    //    System.Threading.Thread.Sleep(50000);
                    //}
                //}
               

                int start1 = strWebContent.IndexOf("收盘为", StringComparison.CurrentCultureIgnoreCase);

                if (start1 == -1)
                {
                    Log(String.Format("未找到股票{0}的收盘价", stock));
                    continue;
                }

                int end = strWebContent.IndexOf("元", start1, StringComparison.CurrentCultureIgnoreCase);

                

                string str = strWebContent.Substring(start1 + 3, (end - start1 - 3));

                decimal v = -77777.77m;
                if (!string.IsNullOrEmpty(str))
                {
                    try
                    {
                        v = decimal.Parse(str);
                    }
                    catch (System.FormatException)
                    {
                        Console.WriteLine("error string : " + str);
                    }
                }

                DataTool.InsertDayGuideLine(stock, 11001, this.dateTimePicker1.Value, v);
                lbStatus.Items.Add(String.Format("stock : {0}", stock));
                lbStatus.SelectedIndex = lbStatus.Items.Count - 1;

            }

            MessageBox.Show("数据获取完成!");
            m_enterReadyState();
        }

        private void m_enterReadyState()
        {
            this.txtBeginStock.Enabled = true;
            this.txtEndStock.Enabled = true;
            this.dateTimePicker1.Enabled = true;
            this.btnStart.Enabled = true;
            this.btnStop.Enabled = false;
        }

        private void label3_Click(object sender, EventArgs e)
        {

        }

        private void txtEndStock_TextChanged(object sender, EventArgs e)
        {

        }

        private void txtBeginStock_TextChanged(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            DataTool.UpdateUnit();
        }

        private void btnStop_Click(object sender, EventArgs e)
        {
            th.Abort();
            m_enterReadyState();
        }
    }
}
